package com.java2007.controller.front;



import com.java2007.controller.BaseServlet;
import com.java2007.entity.CartItem;
import com.java2007.entity.Result;
import com.java2007.factory.BeanFactory;
import com.java2007.pojo.Admin;
import com.java2007.service.OrderService;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 订单控制层
 */
@WebServlet("/order")
public class OrderController extends BaseServlet {
    OrderService orderService= (OrderService) BeanFactory.getBean("com.java2007.service.impl.OrderServiceImpl");
    /**
     * 创建订单
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String generateOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        List<CartItem> list = (List<CartItem>) session.getAttribute("front_cart");
        if (list==null || list.size()<=0){
            return "<script>alert('没有菜品可下单');</script>";
        }
        Long finalPrice = (Long) session.getAttribute("final_price");
        String tableIdStr = (String) session.getAttribute("tableId");
        Integer tableId = Integer.parseInt(tableIdStr);
        Admin admin = (Admin) session.getAttribute("admin");
        //传参，跳转到持久层
        Result result =orderService.save(list,finalPrice,tableId,admin);
        Map<String,Object> map = (Map<String, Object>) result.getData();

        //查询总消费
//        Long money = orderService.getMoney(tableId);
        //存数据到session
        session.setAttribute("orderId",map.get("orderId"));
        session.setAttribute("order_detail_list",map.get("orderDetailList"));
//        session.setAttribute("order_total_price",money);
        session.setAttribute("order_total_price",map.get("total_price"));


        session.removeAttribute("final_price");
        session.removeAttribute("front_cart");
        return "redirect:/front/detail/clientOrderList.jsp";
    }
}
